package cn.zhentao.controller;
import cn.zhentao.pojo.*;
import cn.zhentao.service.PhonAService;
import cn.zhentao.vo.CartVo;
import cn.zhentao.vo.OrderVo;
import cn.zhentao.vo.PageInfoA;
import cn.zhentao.vo.PhoneVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;


@RestController
@RequestMapping("phone")
public class PhoneController {
    @Autowired
    private PhonAService phoneAService;
    @GetMapping("findByPhoneTypeLikePura")
    //模糊查询根据类型  Pura
    public List<Phone> findByPhoneTypeLike() {
        //调用方法
        List<Phone> likePura = phoneAService.findByPhoneTypeLikePura();
        //遍历集合
        for (Phone phone : likePura) {
            Integer id = phone.getId();
            //图片集合
            Img img = phoneAService.findBySrcLikePura(phone.getImgId());
            //设置图片
            phone.setImg(img);
            //放入id 手机表id
            List<Color> colorList = phoneAService.findAllColorByPhoneId(id);
            //设置颜色集合
            phone.setColorList(colorList);
            //查询版本
            List<Version> versionList = phoneAService.findAllVersionByPhoneId(id);
            //设置版本集合
            phone.setVersionList(versionList);
            //遍历集合


        }
        return likePura;
    }
    @GetMapping("findByPhoneTypeLikePocket")
    //模糊查询根据类型  Pocket
    public List<Phone> findByPhoneTypeLikePocket(){
        //调用方法
        List<Phone> likePocket = phoneAService.findByPhoneTypeLikePocket();
        for (Phone phone : likePocket) {
            Integer id = phone.getId();
            Img img = phoneAService.findBySrcLikePocket(phone.getImgId());
            phone.setImg(img);
            List<Color> colorList = phoneAService.findAllColorByPhoneId(id);
            phone.setColorList(colorList);
            List<Version> versionList = phoneAService.findAllVersionByPhoneId(id);
            phone.setVersionList(versionList);
            //遍历集合
//            Price price = phoneAService.findAllPriceByPhoneIdAndVersionId(id, phone.getVersionId());
//            phone.setPrice(price);

        }
        return likePocket;
    };
    @GetMapping("findByPhoneTypeLikeMate")
    //模糊查询根据类型  Mate
    public List<Phone> findByPhoneTypeLikeMate(){
        List<Phone> likeMate = phoneAService.findByPhoneTypeLikeMate();
        for (Phone phone : likeMate) {
            Integer id = phone.getId();
            Img img = phoneAService.findBySrcLikeMate(phone.getImgId());
            phone.setImg(img);
            List<Color> colorList = phoneAService.findAllColorByPhoneId(id);
            phone.setColorList(colorList);
            List<Version> versionList = phoneAService.findAllVersionByPhoneId(id);
            phone.setVersionList(versionList);
            //遍历集合
//                Price price = phoneAService.findAllPriceByPhoneIdAndVersionId(id, phone.getVersionId());
//                phone.setPrice(price);
        }
        return likeMate;
    };
    @GetMapping("findByPhoneTypeLikeNova")
    //模糊查询根据类型  nova
    public List<Phone> findByPhoneTypeLikeNova(){
        List<Phone> likeNova = phoneAService.findByPhoneTypeLikeNova();
        for (Phone phone : likeNova) {
            Integer id = phone.getId();
            Img img = phoneAService.findBySrcLikenova(phone.getImgId());
            phone.setImg(img);
            List<Color> colorList = phoneAService.findAllColorByPhoneId(id);
            phone.setColorList(colorList);
            List<Version> versionList = phoneAService.findAllVersionByPhoneId(id);
            phone.setVersionList(versionList);
            //遍历集合
//                Price price = phoneAService.findAllPriceByPhoneIdAndVersionId(id, phone.getVersionId());
//                phone.setPrice(price);

        }
        return likeNova;
    };
    @GetMapping("findPrice")
    public Price findPrice(int id,int versionId){
        Price price = phoneAService.findAllPriceByPhoneIdAndVersionId(id, versionId);
        return price;
    }
//    @GetMapping("findBySrcLikePura")
//        //模糊查询图片路径 根据Pura
//    public List<Img> findBySrcLikePura(){
//        return ;
//    };
//    @GetMapping("findBySrcLikePocket")
//    //模糊查询图片路径 根据Pocket
//    public List<Img> findBySrcLikePocket(){
//        return phoneAService.findBySrcLikePocket();
//    };
//    @GetMapping("findBySrcLikeMate")
//    //模糊查询图片路径 根据Mate
//    public List<Img> findBySrcLikeMate(){
//        return phoneAService.findBySrcLikeMate();
//    };
//    @GetMapping("findBySrcLikenova")
//    //模糊查询图片路径 根据nova
//    public List<Img> findBySrcLikenova(){
//        return phoneAService.findBySrcLikenova();
//    };
    @GetMapping("findAllMenu")
    public List<Menu> findAllMenu(){
        return phoneAService.findAllMenu();
    }
    @PostMapping("insertCart")
        //添加购物车
    public int insertCart(@RequestBody Cart cart){
        //创建实体类对象
        return phoneAService.insertCart(cart);
    };
    @GetMapping("findAllCartByUserId")
        //查询购物车的所有
    public List<CartVo> findAllCartByUserId(int userId){
        return phoneAService.findAllCart(userId);
    };
    @PutMapping("updateInven")
        //修改库存数量
    public int updateInven(@RequestParam int num,@RequestParam int id){
        return phoneAService.updateInven(num, id);
    };
    @PostMapping("findByPhoneAll")
    public PageInfo<Phone> findByPhoneAll(@RequestBody PageInfoA pageInfoA) {
        PageHelper.startPage(pageInfoA.getPageNum(), pageInfoA.getPageSize());
        //查询所有手机
        List<Phone> phoneAll = phoneAService.findByPhoneAll(pageInfoA);
        //遍历集合
        for (Phone phone : phoneAll) {
            //获取id
            Integer id = phone.getId();
            //设置图片实体类
            phone.setImg(phoneAService.findByImgAllByPhoneId(phone.getImgId()));
            //设置版本集合
            phone.setVersionList(phoneAService.findAllVersionByPhoneId(id));
            //设置颜色集合
            phone.setColorList(phoneAService.findAllColorByPhoneId(id));
            //设置价格集合
            phone.setPriceList(phoneAService.findPriceAllByPhoneId(id,pageInfoA));
        }
        //返回集合
        return new PageInfo<>(phoneAll);
    }
    @DeleteMapping("deletePhone")
        //删除手机的方法根据id 软删除
    public int deletePhone(int id){
        return phoneAService.deletePhone(id);
    };
    @GetMapping("findAllVerison")
    //查询所有版本
    public List<Version> findAllVerison(){
        return phoneAService.findAllVerison();
    }
    @GetMapping("findAllColor")
    //查询所有颜色
    public List<Color> findAllColor(){
        return phoneAService.findAllColor();
    }
    @PostMapping("addPhone")
    @Transactional
    public int addPhone(@RequestBody PhoneVo phoneVo){
        System.out.println(phoneVo+"------------------"+"vo类");
        int i = 0;
        Img img = new Img();
        img.setSrc(phoneVo.getSrc());
        //添加图片表
         phoneAService.insertImg(img);
        Integer id1 = img.getId();
        //添加手机表
        Phone phone = new Phone();
        phone.setImgId(id1);
        phone.setPhoneType(phoneVo.getPhoneType());
        phone.setPhoneInven(phoneVo.getPhoneInven());
        i = phoneAService.insertPhone(phone);
        Integer id = phone.getId();
        //遍历颜色外键
        for (Integer colorId1 : phoneVo.getColorId()) {
            //添加颜色表
            phoneAService.insertPhoneColor(id,colorId1);
            System.out.println("颜色1"+colorId1);
        }
        //遍历版本外键
        Price price = new Price();
        price.setPrices(phoneVo.getPrices());
        //添加价格表
        phoneAService.insertPrice(price);
        //获取价格表主键
        Integer id2 = price.getId();
        //添加价格版本手机中间表
        phoneAService.insertPhonePriceVersion(id,phoneVo.getVersionId(),id2);
        return 1;
    }
    @PostMapping("addVersionAndPrice")
    //添加版本与价格
    @Transactional
    public int addVersionAndPrice(@RequestBody PhoneVo phoneVo){
        //根据手机类型查询
        Phone phone =phoneAService.findByphoneTypePhone(phoneVo.getPhoneType());
        //获取id
        Integer id = phone.getId();
        //创建对象
        Price price = new Price();
        //放入值
        price.setPrices(phoneVo.getPrices());
        //添加价格
        phoneAService.insertPrice(price);
        //获取价格id
        Integer id1 = price.getId();
        //添加中间表
        phoneAService.insertPhonePriceVersion(id,phoneVo.getVersionId(),id1);
        return  1;
    }
    @PutMapping("updatePhone")
    @Transactional
    public int updatePhone(@RequestBody PhoneVo phoneVo){
        //根据手机类型查询
        Phone phone =phoneAService.findByphoneTypePhone(phoneVo.getPhoneType());
        //获取id
        Integer id = phone.getId();
        //修改库存 管理端
        phoneAService.updatephoneInven(phoneVo.getPhoneInven(),id);
        //删除颜色中间表
        phoneAService.deletePhoneColor(id);
        //添加颜色中间表
        for (Integer colorId : phoneVo.getColorId()) {
            phoneAService.insertPhoneColor(id,colorId);
        }
        //根据手机表主键和版本表主键 查询到价格表主键 在根据价格表主键去修改价格
        Integer versionId = phoneVo.getVersionId();
        Price price = phoneAService.findAllPriceByPhoneIdAndVersionId(id, versionId);
        Integer id1 = price.getId();
        phoneAService.updatePrice(phoneVo.getPrices(),id1);
        return 1;
    };
    @GetMapping("findAllMenuAdmin")
        //查询所有管理员菜单表
    public List<MenuAdmin> findAllMenuAdmin(){
        return phoneAService.findAllMenuAdmin();
    };
    @PutMapping("updateAdmin")
    //修改店铺信息
    public int updateAdmin(@RequestBody Address address){
        return phoneAService.updateAdmin(address);
    }
    @DeleteMapping("deleteCart")
    public int deleteCart(@RequestParam int id) {
        System.out.println(id);
        return phoneAService.deleteCart(id);
    }
    @PostMapping("insertOrder")
    //添加订单表的方法
    public int insertOrder(@RequestBody Order order) {
        return phoneAService.insertOrder(order);
    }
    @GetMapping("findAllOrder")
    public List<OrderVo> findAllOrder(int userId) {
        return phoneAService.findAllOrder(userId);
    }
    @PutMapping("updateState")
        //修改订单状态
    public int updateState(int id){
        return  phoneAService.updateState(id);
    };
    @PutMapping("fahuoUpdate")
    public int fahuoUpdate(int id){
        return phoneAService.fahuoUpdate(id);
    }
    @PutMapping("updatetuihuo")
    public int updatetuihuo(int id) {
        return phoneAService.updatetuihuo(id);
    }
    @PutMapping("UpdateTuiHuoAdmin")
    public int UpdateTuiHuoAdmin(int id){
        return phoneAService.UpdateTuiHuoAdmin(id);
    }
    @PutMapping("UpdateRefuseReturn")
    public int UpdateRefuseReturn(int id){
        return phoneAService.UpdateRefuseReturn(id);
    }
    @PutMapping("updateJuShou")
    public int updateJuShou(int id) {
        return phoneAService.updateJuShou(id);
    }
    @GetMapping("findAllOrderAdmin")
    public List<OrderVo> findAllOrderAdmin(){
        return phoneAService.findAllOrderAdmin();
    }
}
